const netWorked = require('../../../utils/netWork.js')
const Config = require('../../../utils/confing.js')
// pages/Archives/Supplier/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    data: [],
    data0: [],
    listdata: [],
    showView: true,
    imgshow: false,
    show: false,
    leibie: "类别",
    imgs: [],
    imgdata: [],
    imgid: 0,//图片id

    pageIndex: 1,//页数
    where: "",//类别条件
    where2: "",//查询条件
    imgid: "0",//图片id

    item_name: "",//页数
    memo: "",//类别条件
    item_barcode: "",//查询条件
    sale_price: "",//图片id
  },
  //类别弹出
  actioncnt: function () {
    let that = this;
    let where = " item_clsno like '__'"
    netWorked.netWork({
      url: 'Archives/Cls_tree?where=' + where,
      method: "POST",
      success: function (res) {

        let data = res.data
        that.setData({
          showView: (!that.data.showView),
          listdata: data,

        })
      }


    })


  },
  //类别选中 
  select: function (e) {
    //  console.log(e.currentTarget.dataset.value);
    var _this = this;
    let where = "item_clsno like '" + e.currentTarget.dataset.id + "%25'";
    netWorked.netWork({
      url: 'WxApplets/SelCommodity?where=' + where,
      method: "POST",

      success: function (res) {

        let data = res.data
        _this.setData({
          leibie: e.currentTarget.dataset.value,
          data: data,
          pageIndex: 1,
          showView: (!_this.data.showView),
          where: where
        })
      }
    })



  },

  //上传图片
  chooseImage(e) {
    let that = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'],  //可选择原图或压缩后的图片
      sourceType: ['album', 'camera'], //可选择性开放访问相册、相机
      success: res => {
        // const images = this.data.images.concat(res.tempFilePaths)
        let img = this.data.imgs;
        this.data.imgs = this.data.imgs.concat(res.tempFilePaths);
        that.setData({
          imgs: this.data.imgs
        })



      }
    })
  },
  //图片删除 
  detImg(e) {
    let that = this
    let index = e.currentTarget.dataset.index;
    let imgid = e.currentTarget.dataset.imgid;

    wx.showModal({
      title: '提示',
      content: '确定要删除嘛？',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: Config.BaseUrl + '/Archives/DelItemImg?id=' + imgid,
            method: "POST",
            success: function (res) {
              // console.log(res.data)

              wx.showToast({
                title: '删除成功',
                icon: "none",
              })

              let goods = that.data.imgdata

              goods.splice(index, 1);
              that.setData({
                imgdata: goods
              })



            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
    // console.log(goods)
  },

  //图片删除 
  clearImg(e) {
    let that = this
    let index = e.currentTarget.dataset.index;
    let goods = this.data.imgs
    goods.splice(index, 1);
    // console.log(imgss)
    that.setData({
      imgs: goods
    })

    // console.log(goods)
  },

  //保存图片
  tijiao() {
    let _this = this;
    let data = this.data.imgs;

    for (var i = 0, h = data.length; i < h; i++) {
      wx.uploadFile({
        url: Config.BaseUrl + 'ImgManger/PreUploadImage',
        filePath: data[i],
        name: 'upload',
        formData: {
          'item_no': _this.data.imgid
        },
        header: {
          "Content-Type": "multipart/form-data"
        },
        success: function (res) {
          wx.request({
            url: Config.BaseUrl + 'Archives/SelItemImg?id=' + _this.data.imgid,
            method: "POST",
            success: function (res) {
              console.log(res.data)
              let data = res.data
              _this.setData({
                imgdata: res.data,
               
              })
            }
          })
          wx.showToast({
            title: '上传成功',
            icon: "none",
          })
          // console.log(res)
        },
        fail: function (res) {
          wx.hideToast();
          wx.showModal({
            title: '错误提示',
            content: '上传图片失败',
            showCancel: false,
            success: function (res) { }
          })
        }
      });
    }

    if (this.data.imgs.length == 0) {
      _this.setData({
        imgshow: false,
      })
    }
    _this.setData({
      imgs: [],
    })
  
  },

  // 关闭图片页面
  imghide: function (e) {
    this.setData({
      imgshow: false,

    })
  },
  // 打开图片页面
  imgshow: function (e) {
    let _this = this

    //console.log(e.currentTarget.dataset.imgid);
    wx.request({
      url: Config.BaseUrl + 'Archives/SelItemImg?id=' + e.currentTarget.dataset.imgid,
      method: "POST",

      success: function (res) {
        console.log(res.data)
        let data = res.data
        _this.setData({
          imgshow: true,
          show: false,
          imgid: e.currentTarget.dataset.imgid,
          imgdata: res.data,
          imgs: [],
        })
      }
    })



  },

  // 关闭详细页面
  hide: function (e) {
    this.setData({
      show: false,
    })
  },
  // 打开详细页面
  show: function (e) {
    let _this = this

    //console.log(e.currentTarget.dataset.supcust_no);
    this.setData({
      show: true,
    })
    _this.setData({
      data0: []
    })
    let id = e.currentTarget.dataset.itemid
    // console.log(id);

    let where = " item_no='" + id + "'";

    wx.request({
      url: Config.BaseUrl + 'WxApplets/SelCommodity?where=' + where,
      method: "POST",
      //data: where=where,
      success: function (res) {


        _this.setData({
          data0: res.data[0]
        })
        console.log(_this.data.data0)
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this;
    netWorked.netWork({
      url: 'WxApplets/SelCommodity',
      method: "POST",
      success: function (res) {


        let data = res.data
        _this.setData({
          data: res.data
        })
      }
    })
  },


  //搜索
  onSearch(e) {

    let where2 = "item_no like '" + e.detail + "%25' or item_name like '%25" + e.detail + "%25' or  item_rem like '%25" + e.detail + "%25' ";
    if (this.data.where != '') {
      where2 = "(" + where2 + ")and " + this.data.where;
    }

    let _this = this
    wx.request({
      url: Config.BaseUrl + 'WxApplets/SelCommodity?where=' + where2,
      method: "POST",
      success: function (res) {

        let data = res.data
        _this.setData({
          data: res.data,
          where2: where2,
          pageIndex: 1
        })
      }
    })
  },
  //设置封面
  handleLongPress(e) {
    console.log("123");
    let _tihs = this
    wx.request({
      url: Config.BaseUrl + 'Archives/UpdIMgCover?id=' + _tihs.data.imgid + '&imgid=' + e.currentTarget.dataset.imginde,
      method: "POST",
      success: function (res) {

        wx.request({
          url: Config.BaseUrl + 'Archives/SelItemImg?id=' + _tihs.data.imgid,
          method: "POST",
          success: function (res) {
            console.log(res.data)
            let data = res.data
            _tihs.setData({
              imgshow: true,
              imgdata: res.data,
            })
          }
        })

        wx.showToast({
          title: '设置封面成功',
          icon: "none",
        })

      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var _this = this;
    _this.setData({
      pageIndex: 1
    })
    //刷新页面
    const pages = getCurrentPages()
    const perpage = pages[pages.length - 1]
    perpage.onLoad()  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

    // 显示加载图标
    // wx.showLoading({
    //   title: '玩命加载中',
    // })
    let index = this.data.pageIndex + 1;
    var _this = this;
    console.log(index)
    let where = this.data.where;
    if (this.data.where2 != '') {
      where = "(" + where + ")and " + this.data.where2;
    }
    wx.request({
      url: Config.BaseUrl + 'WxApplets/SelCommodity?where=' + where + '&pageindex=' + index,
      method: "POST",
      success: function (res) {

        if (res.data != null) {
          let data = res.data
          _this.setData({
            data: _this.data.data.concat(res.data),
            pageIndex: index
          })
        }

      }
    })

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})